package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先构造实体对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //从ThreadLocal中取出当前用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //封装查询结果
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //先判断当前商品是否在购物车当中
        if(list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);
            //在购物车当中则商品数量加一
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //调用数据库更改商品数量
            shoppingCartMapper.updateNumber(shoppingCart1);
        }else {
            //不在购物车当中，直接构造实体类存放入数据库表中
            //先判断是菜品还是套餐
            if(shoppingCart.getDishId() != null) {
                //添加的是菜品
                DishVO dishVO = dishMapper.selectById(shoppingCart.getDishId());
                //添加没有的字段
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());

            } else {
                //添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                //添加没有的字段
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //数量默认为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);
        }


    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 删除购物车的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先构造实体对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //从ThreadLocal中取出当前用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //封装查询结果
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart shoppingCart1 = list.get(0);

        //判断当前商品在购物车之中的数量
        if(shoppingCart1.getNumber() == 1) {
            //如果数量为一，直接从购物车表中删除这个商品
            shoppingCartMapper.deleteOne(shoppingCart1);
        } else {
            //如果数量不为一，那么就先构造实体类，再减少数量字段
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            shoppingCartMapper.updateNumber(shoppingCart1);
        }


    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(currentId);
    }
}



















